#include "SubWidget.h"

SubWidget::SubWidget(QWidget *parent)
    : QWidget(parent)
{
    InitWidget();
}

SubWidget::~SubWidget()
{

}
void SubWidget::InitWidget()
{
    QGroupBox* pGrpboxLocal = new QGroupBox(tr("Subscribe"));
    QLabel *topic = new QLabel(pGrpboxLocal);
    topic->setText(tr("&Topic:"));
    m_topic = new QLineEdit(pGrpboxLocal);
    topic->setBuddy(m_topic);
    m_sub = new QPushButton(tr("Subscribe"));
    QHBoxLayout *phlocalnetcfg = new QHBoxLayout;
    phlocalnetcfg->addWidget(topic);
    phlocalnetcfg->addWidget(m_topic);
    phlocalnetcfg->addWidget(m_sub);
    pGrpboxLocal->setLayout(phlocalnetcfg);
    QVBoxLayout *playout=new QVBoxLayout;
    playout->addWidget(pGrpboxLocal);
    setLayout(playout);

    connect(m_sub,SIGNAL(clicked()),this,SLOT(SendSub()));
}
void SubWidget::SendSub()
{
    char buff[256];
    memset(buff,0,256);
    int len = 0;
    Payload Topic_Name;
    memcpy((char*)&Topic_Name.payload, m_topic->text().toStdString().c_str(), m_topic->text().length());
    int Topiclen = m_topic->text().length();
    Topic_Name.payload[Topiclen] = 0x01;
    Topic_Name.len[0] = HIGHT(Topiclen);
    Topic_Name.len[1] = LOW(Topiclen);
    memcpy(&buff[len], (char*)&Topic_Name, Topiclen + 2 + 1);
    len += Topiclen + 2 + 1;

    m_mq->Subscribe(1234, buff, len);
}
void SubWidget::SetMq(MQ *mq)
{
    m_mq=mq;
}